package com.tools.web.work.captcha;

import com.tools.common.object.Note;

import java.util.Objects;

/**
 * 点选文字的包装实例
 * */
@Note("点选文字的包装实例")
public final class ClickChar {

    @Note("文字字符")
    private char word;

    @Note("文字字符位于验证码图片中的 X 轴坐标")
    private int x;

    @Note("文字字符位于验证码图片中的 Y 轴坐标")
    private int y;

    @Note("允许坐标有多少像素的误差")
    private int faultTolerant;

    public ClickChar(char word, int x, int y) {
        this(word, x, y, 5);
    }

    public ClickChar(char word, int x, int y, int faultTolerant) {
        this.setWord(word);
        this.setX(x);
        this.setY(y);
        this.setFaultTolerant(faultTolerant);
    }

    public static ClickChar ofChar(char word, int x, int y) {
        return new ClickChar(word, x, y);
    }

    public static ClickChar ofChar(char word, int x, int y, int faultTolerant) {
        return new ClickChar(word, x, y, faultTolerant);
    }

    public char getWord() {
        return word;
    }

    public void setWord(char word) {
        this.word = word;
    }

    public int getX() {
        return x;
    }

    public void setX(int x) {
        this.x = x;
    }

    public int getY() {
        return y;
    }

    public void setY(int y) {
        this.y = y;
    }

    public int getFaultTolerant() {
        return faultTolerant;
    }

    public void setFaultTolerant(int faultTolerant) {
        this.faultTolerant = Math.abs(faultTolerant);
    }

    @Override
    public String toString() {
        return "ClickChar{" +
                "word=" + word +
                ", x=" + x +
                ", y=" + y +
                ", faultTolerant=" + faultTolerant +
                '}';
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        ClickChar clickChar = (ClickChar) o;
        if(word != clickChar.word) return false;
        if((y - clickChar.y) > faultTolerant) return false;
        return (x - clickChar.x) <= faultTolerant;
    }

    @Override
    public int hashCode() {
        return Objects.hash(word, x, y, faultTolerant);
    }


    public ClickChar word(char word) {
        this.setWord(word);
        return this;
    }

    public ClickChar x(int x) {
        this.setX(x);
        return this;
    }

    public ClickChar y(int y) {
        this.setY(y);
        return this;
    }

    public ClickChar faultTolerant(int faultTolerant) {
        this.setFaultTolerant(faultTolerant);
        return this;
    }
}
